﻿/************************************************************************************
 *      Copyright (C) 2018 dawenyang,All Rights Reserved
 *      File:
 *                AuthController.cs
 *      Description:
 *            用户操作
 *      Author:
 *                Yangxianwen
 *                
 *                
 *      Finish DateTime:
 *                2018年01月04日
 *      History:
 ***********************************************************************************/

using System;
using System.Web.Mvc;
using FrameWork.Common.Const;
using FrameWork.Context.Oprator;
using FrameWork.Util.Extension;
using FrameWork.Web;
using FrameWork.Web.Attribute;
using MyDb.DoMain.Entity.ViewModel.Account.Auth;

namespace Admin.Web.Areas.Account.Controllers
{
    [HandlerLogin(false)]
    public class AuthController : AdminControllerBase
    {
        /// <summary>
        /// 跳转到登录页面
        /// </summary>
        public ActionResult Login()
        {

            return View();
        }

        /// <summary>
        /// 登录验证
        /// </summary>
        [HttpPost]
        public ActionResult Login(LoginSaveModel loginSaveModel)
        {
            var data = new
            {
                state = CommonConst.JsonStateError,
                message = AuthConst.JsonMessageLoginError
            }.ToJson();

            var viewModel = UserService.Login(loginSaveModel);
            if (viewModel == null)
            {
                return Content(data);
            }
            var opModel = new OperatorModel
            {
                Account = viewModel.Account,
                HeadImg = viewModel.HeadImg,
                LoginTime = viewModel.LoginTime,
                LoginToken = viewModel.LoginToken,
                Phone = viewModel.Phone,
                RoleIdsStr = viewModel.RoleIdsStr,
                UserId = viewModel.UserId,
                UserName = viewModel.Account
            };
            OperatorProvider.Provider.AddCurrent(opModel);
            data = new
            {
                state = CommonConst.JsonStateSuccess,
                message = AuthConst.JsonMessageLoginSuccess
            }.ToJson();
            return Content(data);
        }

        /// <summary>
        /// 首页
        /// </summary>
        [HandlerLogin]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 默认首页
        /// </summary>
        [HandlerLogin]
        public ActionResult Default()
        {
            return View();
        }

        /// <summary>
        /// 退出到登录页面
        /// </summary>
        [HandlerLogin]
        public ActionResult LogOut()
        {
            OperatorProvider.Provider.RemoveCurrent();//移除当前cookie
            return View("Login");
        }
    }
}